package com.dudu.dao;

import java.sql.Connection;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.dudu.bean.CommunicationDataQuerying;
import com.dudu.model.AppExtend;

import core.CoreDao;
import core.db.DBConnectionUtil;

/**
 * 
 * @author jack
 *
 */
public class CommunicationDataDao {

	// 日志输出
	private static final Logger log = Logger.getLogger(CommunicationDataDao.class.getSimpleName());

	public List<JSONObject> queryCommunicationDataQueryingList(CoreDao coreDao, CommunicationDataQuerying bean) {
		Connection con = null;
		List<JSONObject> result = null;
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(
					" select id,product_key,session_id,caller,called,msg_id," +
					" amount,unit,begin_time,end_time,digits,call_state,call_reason,state_time,uuid" +
					" from communication_data_querying where app_key = ? and cust_account = ? ");
			if(StringUtils.isNotBlank(bean.getProduct_key())){
				sql.append(" and product_key = ? ");
			}
			sql.append(" order by id ASC limit 0,1000 ");
			result = coreDao.serviceJsonSql(con, sql.toString(), new Object[] { 
					bean.getApp_key(), bean.getCust_account(), bean.getProduct_key() 
				});

		} catch (Exception ex) {
			ex.printStackTrace();
			log.error("queryCommunicationDataQueryingList 出错：" + ex);
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	
	public int updateCommunicationDataQueryingById(CoreDao coreDao, CommunicationDataQuerying bean) {
		// 数据库连接
		Connection con = null;
		// 最终返回结果
		int result = -1;
		try {
			// 获取数据库连接
			con = DBConnectionUtil.getDBConnection("emate1");
			// 拼写sql
			StringBuffer sql = new StringBuffer(
					" update communication_data_querying set serial_number=? where id=?");

			// 查询获取结果
			List<JSONObject> list = coreDao.serviceJsonSql(con, sql.toString(),
					new Object[] { bean.getSerial_number(), bean.getId() });
			// 获取插入的结果
			if (list != null && list.size() > 0) {
				JSONObject obj = list.get(0);
				result = obj.optInt("result");
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("deleteCommunicationDataQueryingBySerialNumber 出错：" + e);
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	
	public int deleteCommunicationDataQueryingBySerialNumber(CoreDao coreDao, String serialNumber) {
		// 数据库连接
		Connection con = null;
		// 最终返回结果
		int result = -1;
		try {
			// 获取数据库连接
			con = DBConnectionUtil.getDBConnection("emate1");
			// 拼写sql
			StringBuffer sql = new StringBuffer(
					" delete from communication_data_querying where serial_number=? ");
			// 查询获取结果
			List<JSONObject> list = coreDao.serviceJsonSql(con, sql.toString(),
					new Object[] { serialNumber });
			// 获取插入的结果
			if (list != null && list.size() > 0) {
				JSONObject obj = list.get(0);
				result = obj.optInt("result");
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("deleteCommunicationDataQueryingBySerialNumber 出错：" + e);
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	
	public int insertCommunicationDataQueryedBySerialNumber(CoreDao coreDao, String serialNumber) {
		// 数据库连接
		Connection con = null;
		// 最终返回结果
		int result = -1;
		try {
			// 获取数据库连接
			con = DBConnectionUtil.getDBConnection("emate1");
			// 拼写sql
			StringBuffer sql = new StringBuffer("");
			sql.append(" insert into communication_data_queryed( ");
			sql.append(" product_key, app_key, cust_account, session_id, caller, ");
			sql.append(" called, displaynbr, call_state, call_reason, state_time, ");
			sql.append(" amount, unit, begin_time, end_time, digits, "); 
			sql.append(" msg_id, serial_number, create_time, comments, uuid ");
			sql.append(" ) select "); 
			sql.append(" product_key, app_key, cust_account, session_id, caller, "); 
			sql.append(" called, displaynbr, call_state, call_reason, state_time, ");
			sql.append(" amount, unit, begin_time, end_time, digits, "); 
			sql.append(" msg_id, serial_number, create_time, comments, uuid ");
			sql.append(" from communication_data_querying ");
			sql.append(" where serial_number=? ");
			// 查询获取结果
			List<JSONObject> list = coreDao.serviceJsonSql(con, sql.toString(),
					new Object[] { serialNumber });
			// 获取插入的结果
			if (list != null && list.size() > 0) {
				JSONObject obj = list.get(0);
				result = obj.optInt("result");
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("insertCommunicationDataQueryedBySerialNumber 出错：" + e);
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}

	
	
	
	
	
	/**
	 * 根据app标识查询app扩展信息
	 * 
	 * @param appIdentify
	 * @return
	 */
	public AppExtend queryAppExtendByIdentify(CoreDao coreDao, String appIdentify) {
		log.info("AppExtendDao.queryAppExtendByIdentify");
		// 数据库连接
		Connection con = null;
		// 最终返回结果
		AppExtend result = null;

		try {
			// 获取数据库连接
			con = DBConnectionUtil.getDBConnection("emate1");
			// 拼写sql
			StringBuffer sql = new StringBuffer("SELECT * FROM t_app_extend  where app_key=?");
			// 查询获取结果
			List<JSONObject> list = coreDao.serviceJsonSql(con, sql.toString(), new Object[] { appIdentify });
			// 类型转换
			if (list != null && list.size() > 0) {
				// 类型转换
				List<AppExtend> items = coreDao.covertToBean(list, AppExtend.class);
				result = items.get(0);
			}

		} catch (Exception ex) {
			ex.printStackTrace();
			log.error("根据app标识查询App扩展信息出错：" + ex);
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}

	/**
	 * 查询条数（分页用）
	 * 
	 * @param paramSql
	 * @return
	 */
	public int getTotalAcount(CoreDao dao, String paramSql) {
		if (StringUtils.isNotBlank(paramSql)) {
			paramSql = " and " + paramSql;
		}
		Connection con = null;
		int result = -1;
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(
					" select count(1) as totalAcount " + " from t_app_extend" + " where 1=1  " + paramSql);

			List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(), new Object[] {});

			JSONObject obj = list.get(0);
			result = (Integer) obj.get("totalAcount");

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}

	/**
	 * 查询数据（分页）
	 * 
	 * @param paramSql
	 * @param pageSql
	 * @return
	 */
	public List<JSONObject> queryAll(CoreDao dao, String paramSql, String pageSql) {

		Connection con = null;
		List<JSONObject> result = null;
		if (StringUtils.isNotBlank(paramSql)) {
			paramSql = " and " + paramSql;
		}

		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(
					" select *  from t_app_extend where 1=1" + paramSql + " order by create_time desc " + pageSql);
			result = dao.serviceJsonSql(con, sql.toString(), new Object[] { null });

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
}
